import { defineStore } from 'pinia'

export const useAuthStore = defineStore('auth', {
  state: () => ({
    isLoggedIn: false,
    user: null
  }),
  actions: {
    login(userData) {
      this.isLoggedIn = true
      this.user = userData
      // 可以在这里添加将登录状态保存到localStorage的逻辑
      localStorage.setItem('auth', JSON.stringify({ isLoggedIn: true, user: userData }))
    },
    logout() {
      this.isLoggedIn = false
      this.user = null
      localStorage.removeItem('auth')
    },
    initialize() {
      // 从localStorage恢复登录状态
      const authData = localStorage.getItem('auth')
      if (authData) {
        const { isLoggedIn, user } = JSON.parse(authData)
        this.isLoggedIn = isLoggedIn
        this.user = user
      }
    }
  }
})